package cn.tedu.sp02.Controller;

import cn.tedu.sp1.Service.ItemService;
import cn.tedu.sp1.Web.Util.JsonResult;
import cn.tedu.sp1.entity.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
public class ItemController {
    @Autowired
    private ItemService itemService;
    // 获取订单商品列表
    @GetMapping("{orderId}")
    public JsonResult<List<Item>> getItems(
            @PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);
//        if(Math.random() < 0.9 ){
//            int t = new Random().nextInt(10000);
//            Thread.sleep(t);
//            System.out.println("阻塞了"+t+"秒");
//        }
//        System.out.println("跳到了这");
//        return JsonResult.ok().msg("服务器20");
        return JsonResult.ok().data(items);
    }
    // 减少商品库存
    // @RequestBody 完整地接收 post 请求协议体数据
    // [{id:1,name:xxx,number:1},{...},{...}]
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(
            @RequestBody List<Item> items) {
        itemService.decreaseNumber(items);
        return JsonResult.ok().msg("减少库存成功");
    }

}
